{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import IRSwap\n",
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.risk import MarketDataShockBasedScenario, MarketDataPattern, MarketDataShock, MarketDataShockType, IRDeltaParallel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Risk measures are short-hand for a set of predefined scenarios. Let's look at how at to get the `IRDeltaParallel` value using a `MarketDataShockBasedScenario`. We calculate delta as a 2-sided 1bp bump."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swap = IRSwap(PayReceive.Pay, '10y', Currency.USD, notional_amount=10e6)\n",
    "swap.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r_shock_bp = 1/10000\n",
    "ir_scenario_up = MarketDataShockBasedScenario({MarketDataPattern('IR', mkt_asset=Currency.USD): \n",
    "                                               MarketDataShock(MarketDataShockType.Absolute, r_shock_bp)})\n",
    "ir_scenario_down = MarketDataShockBasedScenario({MarketDataPattern('IR', mkt_asset=Currency.USD): \n",
    "                                                 MarketDataShock(MarketDataShockType.Absolute, -r_shock_bp)})\n",
    "with PricingContext(market_data_location='NYC'):\n",
    "    delta = swap.calc(IRDeltaParallel)\n",
    "    with ir_scenario_up:\n",
    "        up = swap.dollar_price()\n",
    "    with ir_scenario_down:\n",
    "        down = swap.dollar_price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined = (up.result() - down.result())/2\n",
    "# should give the same value\n",
    "print(f'Delta direct={delta.result():.0f}, Delta by shocks={((up.result() - down.result())/2):.0f}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
